/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.ProductBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.ProductDao;
import com.minierp.dao.ProductTypeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.UnitDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.Product;
import com.minierp.model.ProductType;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ProductFormCtr extends SimpleFormController {
	private ProductTypeDao productTypeDao;
	private ProductDao productDao;
	private UnitDao unitDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String productTypeId = request.getParameter("type_id");
		refData.put("productTypeId", productTypeId);
		refData.put("productTypeList", this.productTypeDao.getList(true));
		refData.put("unitList", this.unitDao.getList());
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("PRODUCT");
		if (cr == null) {
			throw new Exception("Code rule not found for product");
		} else {
			ProductBean form = new ProductBean();
			form.setCodeRule(cr);
			Product product;
			if (id != null && !id.equals("")) {
				product = (Product) this.productDao.get(Product.class,
						Long.parseLong(id));
				form.setProduct(product);
			} else {
				product = new Product();
				form.setProduct(product);
				String productTypeId = request.getParameter("type_id");
				if (productTypeId != null && !productTypeId.equals("")) {
					form.getProduct().setProductTypeId(
							Long.parseLong(productTypeId));
				}
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		String alertMsg = "";
		ProductBean form = (ProductBean) command;
		Product obj = form.getProduct();
		obj.setPnSpell(PageHelper.getPinYinHeadChar(obj.getProductName())
				.toUpperCase());
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("attach_file");
		String mv;
		String productTypeId;
		String continueFlag;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			mv = multipartFile.getOriginalFilename();
			productTypeId = EncCommonHelper.getExtendName(mv);
			continueFlag = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			String realPath = this.getServletContext()
					.getRealPath(continueFlag);
			String fileName = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + productTypeId;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + fileName));
			obj.setAttachFile(continueFlag + fileName);
		}

		if (id != null && !id.equals("")) {
			this.productDao.update(obj);
			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(Product.class.getName()));
			if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
				mv = "";
				if (form.getCodeRule().getGenerateRule()
						.equals("AUTO_TYPE_CODE")) {
					ProductType productTypeId1 = (ProductType) this.productTypeDao
							.get(ProductType.class, obj.getProductTypeId());
					if (productTypeId1 != null) {
						mv = productTypeId1.getTypeCode();
					}
				}

				obj.setProductCode(PageHelper.generateCode(
						this.serialNumberDao, form.getCodeRule(), mv, 0L));
				if (obj.getProductCode() == null
						|| obj.getProductCode().equals("")) {
					throw new Exception("Generate product code fail!");
				}

				for (Product productTypeId2 = this.productDao.get(
						obj.getProductCode(), true); productTypeId2 != null; productTypeId2 = this.productDao
						.get(obj.getProductCode(), true)) {
					obj.setProductCode(PageHelper.generateCode(
							this.serialNumberDao, form.getCodeRule(), mv, 0L));
				}
			}

			this.productDao.save(obj);
			alertMsg = "增加成功！";
		}

		productTypeId = request.getParameter("type_id");
		if (productTypeId == null) {
			productTypeId = "";
		}

		continueFlag = request.getParameter("continue_flag");
		if (continueFlag == null) {
			continueFlag = "";
		}

		ModelAndView mv1;
		if (continueFlag.equals("1")) {
			mv1 = new ModelAndView("redirect:/base/ProductNew.ihtm?type_id="
					+ productTypeId);
		} else {
			mv1 = new ModelAndView(this.getSuccessView());
			mv1.addObject("returnValue", String.valueOf(obj.getId()));
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv1;
	}

	public ProductTypeDao getProductTypeDao() {
		return this.productTypeDao;
	}

	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public UnitDao getUnitDao() {
		return this.unitDao;
	}

	public void setUnitDao(UnitDao unitDao) {
		this.unitDao = unitDao;
	}
}